#include "stdafx.h"
#include "TestMySPWL.h"
#include "Databasebase.h"
#include "spwltable.h"
#include "DatabaseElement.h"
#include "UeConnectionManager.h"
using namespace PDMS::PDMSDBIF;

CPPUNIT_TEST_SUITE_REGISTRATION( TestMySPWL );


TestMySPWL::TestMySPWL()
{
}


TestMySPWL::~TestMySPWL()
{
		
}

void TestMySPWL::TestRead()
{
	DatabaseBase dbb( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	dbb.DataSource( _T("D:\\SpecificationTemplate.mdb") );
	DatabaseElement *obj = dbb.GetDatabaseElement( _T("SPWL") );	
	ASSERT( obj );
	SPWLTable *pObj = NULL;
	if ( obj->IsKindOf( UE_RUNTIME_CLASS(SPWLTable) ) )
		pObj = static_cast<SPWLTable*>(obj);
	CPPUNIT_ASSERT( pObj );

	UESoft::ErrorStatus es = pObj->Open( _T("ID = 3") );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( pObj->GetID() == 3 );
	CPPUNIT_ASSERT( pObj->Close() == UESoft::eOk );

}

void TestMySPWL::TestInsert()
{
	DatabaseBase dbb( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	dbb.DataSource( _T("D:\\SpecificationTemplate.mdb") );
	
	DatabaseElement *obj = dbb.GetDatabaseElement( _T("SPWL") );	
	ASSERT( obj );
	SPWLTable *pObj = NULL;
	if ( obj->IsKindOf( UE_RUNTIME_CLASS(SPWLTable) ) )
		pObj = static_cast<SPWLTable*>(obj);
	
	UESoft::ErrorStatus es = pObj->Open( _T("All") );
	CPPUNIT_ASSERT( es == UESoft::eOk );

	pObj->NewRecord();
	
	pObj->SetID(11);
	pObj->SetParentID(256);
	pObj->SetLineTypeID(111);
	pObj->SetMaterialID(1111);
	pObj->SetRatingID(11111);
	pObj->SetFluidID(111111);

	pObj->SetSpecName("NAME1");
	pObj->SetMaterialDir("c:\\");
	pObj->SetFluidDir("c:\\");
	pObj->SetLineTypeDir("jkhkj");
	pObj->SetMaterialDir("..........");
	pObj->SetRatingDir("d:\\");

	CPPUNIT_ASSERT( pObj->Update() == UESoft::eOk );
	UESoft::ErrorStatus es1 = pObj->Close();
	CPPUNIT_ASSERT( es1 == UESoft::eOk );


}

void TestMySPWL::TestDelete()
{
	DatabaseBase dbb( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	dbb.DataSource( _T("D:\\SpecificationTemplate.mdb") );

	DatabaseElement *obj = dbb.GetDatabaseElement( _T("SPWL") );	
	ASSERT( obj );
	SPWLTable *pObj = NULL;
	if ( obj->IsKindOf( UE_RUNTIME_CLASS(SPWLTable) ) )
		pObj = static_cast<SPWLTable*>(obj);

	CPPUNIT_ASSERT( pObj );
	UESoft::ErrorStatus es = pObj->Open( _T("All") );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	
	pObj->DeleteRecord( 1 );
	
	CPPUNIT_ASSERT( pObj->Update() == UESoft::eOk );
	CPPUNIT_ASSERT( pObj->Close() == UESoft::eOk );

}

void TestMySPWL::TestUpdate()
{
	DatabaseBase dbb( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	dbb.DataSource( _T("D:\\SpecificationTemplate.mdb") );
	
	DatabaseElement *obj = dbb.GetDatabaseElement( _T("SPWL") );	
	ASSERT( obj );
	SPWLTable *pObj = NULL;
	if ( obj->IsKindOf( UE_RUNTIME_CLASS(SPWLTable) ) )
		pObj = static_cast<SPWLTable*>(obj);

	UESoft::ErrorStatus es = pObj->Open();
	CPPUNIT_ASSERT( es == UESoft::eOk );

	pObj->UpdateRecord(4);
	
	pObj->SetFluidID(123);
	pObj->SetLineTypeDir( _T("sdf") );

	CPPUNIT_ASSERT ( pObj->Update() == UESoft::eOk );
	CPPUNIT_ASSERT( pObj->Close() == UESoft::eOk );

}